//index.js
//获取应用实例
const utils = require('../../utils/util.js');
const app = getApp();
Page({
    data: {
        loginFlag:false,
        //判断小程序的API，回调，参数，组件等是否在当前版本可用。
        canIUse: wx.canIUse('button.open-type.getUserInfo'),
        count: 0,
        pindex: 1,
        param:"",
        act: "getzx", //默认是最新
        bannel: [],
        indicatorDots: true,
        autoplay: true,
        interval: 3000,
        duration: 500,
        circular: true,

        //中间的选择器
        product_recommend: [
            {
            name: '最新发布',
            id: 1,
            selected: 'selected'
            }
            ,
            {
                name: '最热',
                id: 3,
                selected: ''
            }
        ],
        product_list: [],
    },
    bannelclick: function (e) {
        wx.navigateTo({
          url: '../vote/detail?id=' + e.currentTarget.dataset.id,
        })
        
    },
     //  / pages / product / details ? id =
    //页面上拉触底事件的处理函数
    onReachBottom: function () {

        //需要去请求数据
        this.test();
        // var $this = this;
        // $this.data.pindex = $this.data.pindex + 1;

        // console.log('ppppp='+$this.data.pindex);
        // //需要去请求数据
        // utils.geturl({
        //     "handler": "Home",
        //     "method":"homeData",
        //     "param": '{"p":"' + $this.data.pindex + '","act":"'+$this.data.act+'"}',
        // }, function (res) {
        //     if (res.code == 200) {

        //         if (res.data.obj.vote.length == 0) {
        //             wx.showToast({
        //                 title: '已无更多数据',
        //                 icon: "none"
        //             });
        //         }
        //         for (var i = 0; i < res.data.obj.vote.length; i++) {
        //             $this.data.product_list.push(res.data.obj.vote[i]);
        //         }
        //         // $this.data.product_list.push(res.data.plist);
        //         $this.setData({
        //             "product_list": $this.data.product_list
        //         });
        //     } else {
        //         wx.showToast({
        //             title: '服务器错误,请稍候再试',
        //             icon: "none"
        //         });
        //     }
        // });

    },

    onLoad: function () {

        var that = this;
        // 查看是否授权
        wx.getSetting({
            success: function (res) {
                if (res.authSetting['scope.userInfo']) {
                    wx.getUserInfo({
                        success: function (res) {
                            //从数据库获取用户信息
                           // that.queryUsreInfo();
                            //用户已经授权过
                            wx.switchTab({
                                url: '/pages/index/index'
                            })
                        }
                    });
                }
            }
        });
        var $this = this;
        $this.getHomeData();

         //需要去请求数据
         this.test();

         // 登录获取token 
         this.login();

    },

     //获取用户信息接口
     queryUsreInfo: function () {
         console.log(app.globalData.urlPath + 'user/userInfo');
        wx.request({
            url: app.globalData.urlPath + 'user/userInfo',
            data: {
                openid: app.globalData.openid
            },
            header: {
                'content-type': 'application/json'
            },
            success: function (res) {
                console.log(res.data);
                getApp().globalData.userInfo = res.data;
            }
        })
    },

    //获取数据
    getHomeData: function(){
        // wx.showLoading({
        //     title: '玩命加载中',
        // });
        var $this = this;

        let isAjax = true;
        //需要去请求数据

        if (isAjax){
            isAjax = false;
            // utils.geturl({
            //     "handler": "Home",
            //     "method":"homeData",
            //     "param": '{"p":"' + $this.data.pindex + '","act":"'+$this.data.act+'"}',
            // }, function (res) {
            //     console.log(res);
            //     console.log(typeof res);

            //     $this.setData({
            //         "bannel": res.data.obj.banner,
            //         "produty_type": res.data.obj.vote_type,
            //         "product_list": res.data.obj.vote,
            //         "count": res.data.obj.count
            //     });
            //     isAjax = true;

            // });
        }
    },

    //推荐点击
    recclick: function (e) {


        
     

        var $this = this;
        for (var item in this.data.product_recommend) {

            if (this.data.product_recommend[item].id == e.target.dataset.id && this.data.product_recommend[item].selected == "") {
                this.data.product_recommend[item].selected = 'selected';

                if (e.target.dataset.id == 1) {
                    $this.data.param = '{"act":"new","p":"1"}';
                    $this.data.pindex = 1;
                    $this.data.act = 'new';
                }else if (e.target.dataset.id == 3) {
                    $this.data.param = '{"act":"mj","p":"1"}';
                    $this.data.pindex = 1;
                    $this.data.act = 'mj';
                }
            } else {
                this.data.product_recommend[item].selected = '';
            }
        }

        // console.log(this.data.product_recommend);
        // Page.prototype.setData();
        this.setData({
            "product_recommend": this.data.product_recommend
        });
        console.log($this.data.act);
        if ($this.data.act != "") {

            let isAjax = true;

            if (isAjax){

                isAjax = false;

                // let data1 = [{
                //     "cover_url":"www.baidu.com",
                //     "title":"测试",
                //     "content":"测试",
                // },{
                //     "cover_url":"www.baidu.com",
                //     "title":"测试",
                //     "content":"测试",
                // },{
                //     "cover_url":"www.baidu.com",
                //     "title":"测试",
                //     "content":"测试",
                // }]

                // $this.setData({
                //     "product_list": data1
                // })

                this.test();
                console.log(this.product_list);
                //需要去请求数据
                // utils.geturl({
                //     "handler": "Home",
                //     "method":"homeData",
                //     "param": $this.data.param
                // }, function (res) {
                //     console.log(res)
                //     if (res.code == 200) {
                //         $this.setData({
                //             "product_list": res.data.obj.vote
                //         });
                //     } else {
                //         wx.showToast({
                //             title: '服务器错误,请稍候再试',
                //             icon: "none"
                //         });
                //     }
                //     isAjax = true;
                // });
            }
        }
    },
    //类型点击
    typeclick: function (e) {
        console.log(e);
        wx.navigateTo({
            url: '/pages/product/list?id=' + e.target.dataset.id,
        })
    },
    productclick: function () {
        console.log(Math.random());
    },

    test:function(){

       let  header = {
            'Authorization': wx.getStorageSync('Set-Cookie')
         };
         console.log("222222",header);
        wx.request({
            url: 'http://localhost/dev-api//system/event/list',
            method: 'GET',
            header:header,
            data: {"pageSize":"10","pageNum":"1"},
            success: (res) => {
           console.log(res);
             console.log(res.data.rows)
              this.setData({
                 product_list: res.data.rows
             });

            }
          })

    },
    login:function(){
        console.log("登录方法执行");
        wx.request({
            url: 'http://localhost/dev-api/login',
            method: 'POST',
            header:{t:"1111"},
            data: {"username":"admin","password":"admin123"},
            success: (res) => {
            console.log(res);
            console.log(res.data.token);

            if (res.statusCode == 200) {
                if (res.header['Set-Cookie'] != '') {
                   wx.setStorageSync('Set-Cookie', res.data.token)
                }
             }
            }
          })
    }

})